package org.jeecg.common.modules.xetuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.modules.wx.entity.WxAccount;
import org.jeecg.common.modules.xetdoctor.entity.XetDoctor;
import org.jeecg.common.modules.xetdoctor.service.IXetDoctorService;
import org.jeecg.common.modules.xetuser.entity.XetUser;
import org.jeecg.common.modules.xetuser.mapper.XetUserMapper;
import org.jeecg.common.modules.xetuser.service.IXetUserService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Description: xet_user
 * @Author: jeecg-boot
 * @Date:   2021-01-27
 * @Version: V1.0
 */
@Service
@Slf4j
public class XetUserServiceImpl extends ServiceImpl<XetUserMapper, XetUser> implements IXetUserService {
    private String goXeStudioUrl = "https://appigc72vmw2720.h5.xiaoeknow.com/mp/";
    @Resource
    private IXetDoctorService xetDoctorService;
    @Resource
    private IXetUserService xetUserService;
    @Resource
    private XetUserMapper xetUserMapper;

    @Override
    public XetUser checkOpenIdPage(String openid,String pageId){
        LambdaQueryWrapper<XetUser> query = Wrappers.lambdaQuery();
        query.eq(XetUser::getOpenid,openid);
        query.eq(XetUser::getXePageId,pageId);
        return getOne(query);
    }

    @Override
    public List<XetDoctor> getDoctorsByOpenid(String openId){
//        LambdaQueryWrapper<XetUser> query = Wrappers.lambdaQuery();
//        query.eq(XetUser::getOpenid,openid);
//        query.orderByDesc(XetUser::getCreateTime);
//        List<XetUser> xetUsers = list(query);
//        log.info("数据：{}",String.valueOf(xetUsers));
//        if(xetUsers.size() > 0){
//            List<String> pageIdList = new ArrayList<>();
//            for(XetUser xetUser:xetUsers){
//                pageIdList.add(xetUser.getXePageId());
//            }
//            List<XetDoctor> doctors = xetDoctorService.getDoctorsByPageId(pageIdList);
//            log.info("医生数据：{}",String.valueOf(doctors));
//            return doctors;
//        }

        return xetUserMapper.getDoctorsByOpenid(openId);
    }

    @Override
    public String xetUrl(String openId,String pageId){
        XetUser xetUserCheck = this.checkOpenIdPage(openId,pageId);
        log.info("页面和openid关系：{}",xetUserCheck);
        if(Objects.nonNull(xetUserCheck)){
            // 存在关系
            xetUserCheck.setUpdateTime(new Date());
            xetUserService.updateById(xetUserCheck);
        }else{
            // 不存在关系：创建关系
            XetUser xetUser = new XetUser();
            xetUser.setOpenid(openId);
            xetUser.setXePageId(pageId);
            xetUser.setCreateTime(new Date());
            xetUser.setUpdateTime(new Date());
            xetUserService.save(xetUser);
        }
        return goXeStudioUrl + pageId;
    }
}
